use rcli::{process_csv, process_decode, process_encode, process_genpass, process_http_serve, process_text_sign, Base64SubCommand, Opts, SubCommand, TextSignFormat, TextSignOpts, TextSubCommand};
use clap::Parser;

#[tokio::main]
async fn main() ->anyhow::Result<()> {
    tracing_subscriber::fmt::init();
    let opts : Opts = Opts::parse();
    match opts.cmd {
        SubCommand::Csv(csv_opts) => {
                        //   let mut output = csv_opts.output.unwrap_or_else(|| "output.json".to_string());
                           let  output = if let Some(output) = csv_opts.output {
                                output.clone()
                            } else {
                              // "output.json".into()
                              format!("output.{}", csv_opts.format)
                                // match csv_opts.format {
                                //     rcli::opts::OutputFormat::Json => "output.json".to_string(),
                                //     rcli::opts::OutputFormat::Yaml => "output.yaml".to_string(),
                                //     rcli::opts::OutputFormat::Toml => "output.toml".to_string(),
                                // }
                            };

                          process_csv(&csv_opts.input, output,csv_opts.format)?;
                        }
        SubCommand::GenPass(gen_pass_opts) => {
                          //  process_genpass(gen_pass_opts.length,gen_pass_opts.uppercase,
                          // gen_pass_opts.uppercase,gen_pass_opts.number,gen_pass_opts.symbol)?;
                          process_genpass(&gen_pass_opts)?;
                        }
        SubCommand::Base64(subcmd) =>  match subcmd {
                        Base64SubCommand::Encode(opts) => {
                          process_encode(&opts.input, opts.format)?;

                        }
                        Base64SubCommand::Decode(opts) => {
                          process_decode(&opts.input,opts.format)?;
                        }
                    }
        SubCommand::Text(subcmd) => match subcmd {
                TextSubCommand::Sign(opts) => match opts.format {
                            TextSignFormat::Blake3 => 
                              process_text_sign(&opts.input,&opts.key, opts.format)?,
                            TextSignFormat::Ed25519 => {
                              println!("sign with ed25519");
                            },
                    
                      }
                TextSubCommand::Verify(opts) => {
                  print!("{:?}",opts);
                   }
              }
        SubCommand::Http(subcmd) => match subcmd{
            rcli::HttpSubCommand::Serve(opts) => {
            //  println!("{:?}",opts);
            //   println!("Serving at http://0.0.0.0:{}",opts.port);
             process_http_serve(opts.dir, opts.port).await?;
            },
        },
              }


    Ok(())
}


